
import axios from "axios";
const ConfigBaseURL = "http://localhost:3000"
// const ConfigBaseURL = "/api"

// 使用create方法创建axios实例
const Service = axios.create({
    baseURL: ConfigBaseURL,//默认地址
    timeout: 7000//超时
})


//3. 给post请求添加请求头设置（不同项目，值不一样）
// Service.defaults.headers.post["Content-Type"] = "application/json;charset=UTF-8";
Service.defaults.headers.post["Content-Type"] = "application/json;charset=UTF-8";
// Service.defaults.headers['authorization'] = `Bearer ${token}`


//4.1 添加请求拦截器
Service.interceptors.request.use(
    request => {
        // loadingInstance = Loading.service({
        //     lock: true,
        //     text: 'loading...'
        // })
        if (localStorage.getItem('token')) {
            request.headers.token = localStorage.getItem('token')
            // request.headers.Authorization = `Bearer `+ localStorage.getItem('token')
            console.log('token:' + localStorage.getItem('token'));
        }
        console.log("axios请求拦截");
        return request
    },
    err => {
        return Promise.reject(err);
    }
)

//4.2 添加响应拦截器
Service.interceptors.response.use(
    response => {
        // loadingInstance.close()
        console.log("axios响应拦截");
        return response
    },
    err => {
        // console.log("TCL: error", err);
        // const mag = err.Message !== undefined ? err.Message : ''
        // Message({
        //     message:'网络错误' + msg,
        //     type: 'error',
        //     duration: 3 * 1000
        // })
        // loadingInstance.close()
        return Promise.reject(err)
    }
)

export default Service 